import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.util.PDFTextStripper;


public class Item extends Seccion{
		
	private String paginaInicial;
	private String paginaFinal;
	
	
	@Override
	protected String getPaginaInicial() {
		return paginaInicial;
	}
	@Override
	protected void setPaginaInicial(String paginaInicial) {
		this.paginaInicial = paginaInicial;
	}
	@Override
	protected String getPaginaFinal() {
		return paginaFinal;
	}
	@Override
	protected void setPaginaFinal(String paginaFinal) {
		this.paginaFinal = paginaFinal;		
	}
	@Override
	protected boolean isSeccionSimple() {
		return true;
	}
	
	@Override
	protected Seccion parse(PDOutlineItem item, PDDocument doc, PDOutlineItem tope) {
		 PDFTextStripper stripper;
		try {
			stripper = new PDFTextStripper();		
			 stripper.setStartBookmark(item);
			 stripper.setEndBookmark(item.getNextSibling());
			 String redundante = "";
			 PDFTextStripper stripperAux = new PDFTextStripper();
			 if(item.getNextSibling() == null && tope != null){
				 stripperAux.setStartBookmark(tope);
				 stripperAux.setEndBookmark(tope);
				 redundante = stripperAux.getText(doc);
			 }else{
				 if(item.getNextSibling() == null){ 
					 stripperAux.setStartBookmark(item.getNextSibling());
					 stripperAux.setEndBookmark(item.getNextSibling());
					 redundante = stripperAux.getText(doc);
				 }
			 }
			 String original = stripper.getText(doc);
			 
			 super.setTexto(original.substring(0, original.length() - redundante.length()));
			 
			 super.setName(item.getTitle());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this;
	}
	@Override
	protected String read() {
		return this.getTexto() + '\n';
	}
}
